<script setup>
import { ref } from 'vue'

const msg = ref('')
const dragEle = ref(null)

let dragSrcEle = null
const onDragStart = (evt) => {
    msg.value = '开始拖拽' + evt.target.innerText

    dragSrcEle = evt.target
    evt.dataTransfer.setData('text', evt.target.innerText)
    evt.dataTransfer.setDragImage(dragEle.value, 0, 0)
}

const onDragEnd = (evt) => {
    console.log('drag end')
    msg.value = '拖拽结束'
}

const onDragEnter = (evt) => {
    console.log('drag enter')
    msg.value = '进入拖拽区域'
}

const onDragLeave = (evt) => {
    console.log('drag leave')
    msg.value = '离开拖拽区域'
}

const onDragOver = (evt) => {
    console.log('drag over')
    msg.value = '拖拽中'
}

const onDrop = (evt) => {
    console.log(evt)
    msg.value = '拖拽释放'

    evt.target.appendChild(dragSrcEle)
}
</script>

<template>
    <div class="drag-wrapper">
        <div class="container-1">
            <div 
                class="drag-container"
                @dragover.prevent="onDragOver"
                @drop.prevent="onDrop"
            >
                <div 
                    class="drag-item" 
                    draggable="true"
                    @dragstart="onDragStart"
                    @dragend="onDragEnd"
                    @dragover.stop
                >1</div>
                <div 
                    class="drag-item" 
                    draggable="true"
                    @dragstart="onDragStart"
                    @dragend="onDragEnd"
                    @dragover.stop
                >2</div>
                <div 
                    class="drag-item" 
                    draggable="true"
                    @dragstart="onDragStart"
                    @dragend="onDragEnd"
                    @dragover.stop
                >3</div>
            </div>

            <div
                class="drop-container"
                draggable="true"
                @dragenter="onDragEnter"
                @dragleave="onDragLeave"
                @dragover.prevent="onDragOver"
                @drop.prevent="onDrop"
            >

            </div>
        </div>

        <div ref="dragEle" style="width: 100px;padding: 10px;border: 1px solid #ccc; background: #eee;">{{ msg }}</div>
        <div>{{ msg }}</div>
    </div>
</template>

<style scoped>
.drag-wrapper {
    height: 100%;
    padding: 10px;
    overflow: auto;
}

.container-1 {
    display: flex;
    gap: 50px;
}

.drag-container,
.drop-container {
    display: flex;
    flex-wrap: wrap;
    align-content: flex-start;
    gap: 10px;
    padding: 10px;
    flex: 1;
    height: 300px;
    overflow: hidden;
    border: 1px solid #ccc;
}

.drag-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50px;
    height: 50px;
    background-color: #f0f0f0;
    cursor: move;
    user-select: none;
}
</style>